<template>
    <div class="platformTemp">
        <el-form class="formBox" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="true"
            label-width="68px">
            <el-form-item label="医院" prop="hospital" clearable>
                <department :isAllDept="true" :isHospital="true" :isdepat="true" :isClearDept="isClearDept" @refreshData="refreshData"
                    deptWidth="200px" ref="department"></department>
            </el-form-item>
            <el-form-item label="适用专病" prop="diseaseId" clearable>
                <el-select v-model="queryParams.diseaseId" placeholder="请选择专病名称" clearable filterable style="width: 200px">
                    <el-option v-for="item in classifyList" :key="item.diseaseClassifyId" :label="item.diseaseClassifyName"
                        :value="item.diseaseClassifyId"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="项目类别" prop="planCategoryId" clearable>
                <el-select v-model="queryParams.planCategoryId" placeholder="请选择方案大类" clearable filterable
                    style="width: 200px">
                    <el-option v-for="dict in dict.type.sys_plan_category" :key="dict.value" :label="dict.label"
                        :value="dict.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="方案名称" prop="medicalEquipment">
                <el-input v-model="queryParams.medicalEquipment" placeholder="请输入方案名称" clearable style="width:200px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <div style="margin-bottom: 20px;">
            <el-form :model="form" ref="addqueryForm" size="small" :inline="true" v-show="true" label-width="80px"
                :rules="rules">
                <el-form-item label="使用科室" prop="deptId" style="margin-bottom:0px;">
                    <department :isAdd="true" @changedepatData="changedepatData" :isdisabled="false" deptWidth="230px">
                    </department>
                </el-form-item>
                <el-form-item>
                    <el-button @click="comfirm()" type="success" size="small" icon="el-icon-plus" plain
                        :disabled="multiple">批量引用</el-button>
                </el-form-item>
            </el-form>


        </div>
        <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
            @selection-change="handleSelectionChange" border>
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column label="医院" align="center" prop="hospitalName" :show-overflow-tooltip="true" width="150">
                <template slot-scope="scope">
                    <departTag :options="deptNameLists" :value="scope.row.hospitalId" />
                </template>
            </el-table-column>
            <el-table-column label="科室" align="center" prop="deptName" :show-overflow-tooltip="true" width="150">
                <template slot-scope="scope">
                    <departTag :options="deptNameLists" :value="scope.row.deptId" />
                </template>
            </el-table-column>
            <el-table-column sortable label="适用专病" align="center" prop="diseaseName" :show-overflow-tooltip="true" />
            <el-table-column sortable label="项目类别" align="center" prop="planCategoryId" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span>{{ matchDict("sys_plan_category", scope.row.planCategoryId) }}</span>
                </template>
            </el-table-column>
            <el-table-column sortable label="方案名称" align="center" prop="medicalEquipment" :show-overflow-tooltip="true" />
            <el-table-column sortable label="用法" align="center" prop="usageId" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span>{{ matchDict("sys_plan_usage", scope.row.usageId) }}</span>
                </template>
            </el-table-column>
            <el-table-column sortable label="用量及单位" align="center" prop="dosage" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span>{{ scope.row.dosage ? scope.row.dosage + matchDict("sys_dosage_unit", scope.row.dosageUnit) : ''
                    }}</span>
                </template>
            </el-table-column>
            <el-table-column sortable label="频次" align="center" prop="frequencyId" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span>{{ matchDict("sys_dosage_frequency", scope.row.frequencyId) }}</span>
                </template>
            </el-table-column>
            <el-table-column sortable label="默认数量" align="center" prop="num" :show-overflow-tooltip="true" />
            <el-table-column sortable label="单价" align="center" prop="price" :show-overflow-tooltip="true" />
            <el-table-column sortable label="创建时间" align="center" prop="createTime" width="160">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" @click="handleEdit(scope.$index, scope.row)">引用</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />
    </div>
</template>

<script>

import { listPlatformPlan, copyPlatformPlan } from "@/api/system/plan";
import { listClassify } from "@/api/system/classify";
import { deptNameList } from "@/api/system/dept";
import department from "@/views/components/department/index.vue";
import departTag from "@/views/components/department/departTag.vue";

export default {
    name: "platformTemp",
    dicts: [
        'sys_plan_category',
        "sys_dosage_unit",
        "sys_dosage_frequency",
        "sys_plan_usage",
        "sys_picking_location",
    ],
    components: { department, departTag },
    props: ['type'],//type 1表单  2 单项  3专病   4 设备  5 检测指标  6 文本
    data() {
        return {
            deptNameLists: [],
            // 专病管理列表
            classifyList: [],
            tableData: [],
            multipleSelection: [],
            // 非多个禁用
            multiple: true,
            form: {
                deptId: undefined,
                hospitalId: undefined
            },
            total: 0,
            queryParams: {
                diseaseId: undefined,
                planCategoryId: undefined,
                medicalEquipment: undefined,
                pageNum: 1,
                pageSize: 10
            },
            rules: {
                deptId: [
                    {
                        required: true,
                        message: "科室必选",
                        trigger: "blur",
                    },
                ],

            },
          isClearDept:'',
        }
    },
    mounted() {
        this.getdeptNameList();
        this.getClassifyList()
    },
    beforeDestroy() {

    },
    methods: {
        refreshData(data) {
            this.queryParams.pageNum = 1;
            this.queryParams.hospitalId = data.hospitalId
            this.queryParams.deptId = data.deptId
            this.getdeptNameList();
        },
        // 回显医院及门诊
        getdeptNameList() {
            deptNameList().then((response) => {
                this.deptNameLists = response.data;
                this.getList();
            });
        },
        changedepatData(data) {
            console.log(data)
            if (data && data.length > 0) {
                this.form.deptId = data[0].deptId
                this.form.hospitalId = data[0].parentId
            } else {
                this.form.deptId = undefined
                this.form.hospitalId = undefined
            }
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams = {
                diseaseId: undefined,
                planCategoryId: undefined,
                medicalEquipment: undefined,
                pageNum: 1,
                pageSize: 10
            }
          this.isClearDept=Date.now()
          this.resetForm("queryForm");
          this.handleQuery()
        },
        /** 查询专病列表 */
        getClassifyList() {
            listClassify(
                {
                    pageNum: 1,
                    pageSize: 9999,
                    status: '0',
                }
            ).then((response) => {
                this.classifyList = response.rows;

            });
        },
        getList() {
            listPlatformPlan(this.queryParams).then((response) => {
                this.tableData = response.rows;
                this.tableData.forEach(item => {
                    if (item.parameters) {
                        item.parameters = this.getParametersValue(item.parameters)
                    }
                })
                this.total = response.total
            });
        },
        getParametersValue(row) {
            let text = ''
            JSON.parse(row).forEach((item, index) => {
                text += item.label + '(' + item.value + ');'
            })
            return text
        },
        /** id转化成对应的文字 */
        matchDict: function (key, id) {
            let _this = this;
            const result = _this.dict.type[key];
            let textValue = "";
            result.forEach((item) => {
                if (item.raw.dictValue == id) {
                    textValue = item.raw.dictLabel;
                }
            });
            return textValue;
        },
        toggleSelection() {
            this.$refs.multipleTable.clearSelection();
        },
        handleEdit(index, row) {
            console.log(index, row);
            this.$refs["addqueryForm"].validate((valid) => {
                if (valid) {
                    copyPlatformPlan(row.planId, this.form.deptId, this.form.hospitalId).then((response) => {
                        if (response.code === 200) {
                            this.$message.success('引用成功')
                            this.$emit('refresh')
                        }
                    })
                }
            });

        },
        handleSelectionChange(selection) {
            this.multipleSelection = selection.map((item) => item.planId);
            this.multiple = !selection.length;
            console.log(this.multipleSelection)
        },
        comfirm() {
            this.$refs["addqueryForm"].validate((valid) => {
                if (valid) {
                    let Mdata = ''
                    this.multipleSelection.forEach((item, index) => {
                        if (index + 1 < this.multipleSelection.length) {
                            Mdata += item + ','
                        } else {
                            Mdata += item
                        }
                    })
                    copyPlatformPlan(Mdata, this.form.deptId, this.form.hospitalId).then((response) => {
                        if (response.code === 200) {
                            this.$message.success('引用成功')
                            this.$emit('refresh')
                        }
                    })
                }
            });
        }
    },

}
</script>

<style scoped  lang="scss">

</style>
